package com.wwj.tem.utils;

import com.wwj.tem.constant.HttpStatus;
import com.wwj.tem.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author: wwj
 * @create: 2024/7/24 - 22:03
 * Description:
 */
public class BeanCopyUtils {
	
	private BeanCopyUtils() {
	}
	
	public static <V> V copyBean(Object source,Class<V> clazz) {
		// 创建目标对象
		V result;
		try {
			result = clazz.newInstance();
			// 实现属性copy
			BeanUtils.copyProperties(source, result);
		} catch (Exception e) {
			throw new BusinessException(HttpStatus.ERROR, "系统内部异常");
		}
		// 返回结果
		return result;
	}
	
	
	
	public static <O,V> List<V> copyBeanList(List<O> list, Class<V> clazz){
		return list.stream()
				.map(o -> copyBean(o, clazz))
				.collect(Collectors.toList());
	}
	
	public static <V> V copyPropertiesIgnoreNull(Object source,Class<V> clazz){
		// 创建目标对象
		V result = null;
		try {
			result = clazz.newInstance();
			// 实现属性copy
			BeanUtils.copyProperties(source, result, getNullPropertyNames(source));
		} catch (Exception e) {
			throw new BusinessException(HttpStatus.ERROR, "系统内部异常");
		}
		// 返回结果
		return result;
	}
	
	public static String[] getNullPropertyNames (Object source) {
		final BeanWrapper src = new BeanWrapperImpl(source);
		java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
		
		Set<String> emptyNames = new HashSet<>();
		for(java.beans.PropertyDescriptor pd : pds) {
			Object srcValue = src.getPropertyValue(pd.getName());
			if (srcValue == null) {
				emptyNames.add(pd.getName());
			}
		}
		String[] result = new String[emptyNames.size()];
		return emptyNames.toArray(result);
	}
}
